/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Factory;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Felipe
 */
public class ConnectionFactory {
    private static ConnectionFactory conectionFactory;
    private Connection connection;
    private Properties properties = new Properties();
    String urlProperties = "../Configuration/Connection.properties";

    public static ConnectionFactory getInstance(){
        if(conectionFactory==null){
            conectionFactory =  new ConnectionFactory();
        }
        return conectionFactory;
    }
    
    public Connection getConnection(){
        if(connection==null){
            try{
                properties.load(this.getClass().getResourceAsStream(urlProperties));
                String connection_url = properties.getProperty("url");
                String user = properties.getProperty("user");
                String password = properties.getProperty("password");
                DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                connection = DriverManager.getConnection(connection_url, user, password);
            }catch (SQLException ex) {
                Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return connection;
    }
}

